package com.yk.airline.process;

import com.yk.airline.model.AirLineModel;
import com.yk.airline.model.CityModel;
import com.yk.airline.model.OriginDataModel;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Project Name:airlinecrawler
 * File Name:Xiecheng
 * Package Name:com.yk.airline.process.xiecheng
 * Date:2018/1/30 8:53
 * Author:zhangshaoyang
 * Description:
 * Copyright (c) 2018, 重庆云凯科技有限公司 All Rights Reserved.
 */
@Component
public class Xiecheng {
    public static void main(String[] args) throws InterruptedException, ParseException {
        xiecheng();
//        tongcheng();
//        feizhu();
    }

    public static String[] getDateAfter(String specifiedDay) throws ParseException {
        int days[] = {0, 2, 6, 16};
        String dayAfters[] = new String[4];
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        for (int i = 0; i < 4; i++) {
            now.set(Calendar.DATE, now.get(Calendar.DATE) + days[i]);
            String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(now.getTime());
            dayAfters[i] = dayAfter;
            System.out.println(dayAfters[i]);
        }
        return dayAfters;
    }

    private static void xiecheng() throws InterruptedException, ParseException {
        System.setProperty("webdriver.gecko.driver", "C:\\WorkEnvir\\geckodriver\\geckodriver.exe");
        String baseUrl = "http://flights.ctrip.com/booking/CTU-CAN-day-1.html?DDate1=";
        String timeList[] = getDateAfter("2018-02-02");
        for (String time : timeList) {
            FirefoxDriver webDriver = new FirefoxDriver();
            String url = baseUrl + time;
            webDriver.get(url);
            List<WebElement> search_box = webDriver.findElementsByCssSelector(".search_box.search_box_tag.search_box_light");
            for (WebElement element : search_box) {
                element.findElement(By.cssSelector(".J_expandBtn.btn_book.arrow_down")).click();
                element.findElements(By.className("arrow_down")).forEach(WebElement::click);
            }
            Document pageSource = Jsoup.parse(webDriver.getPageSource());
            System.out.println(pageSource);
            webDriver.close();
        }
    }

    private static void tongcheng() {
        System.setProperty("webdriver.gecko.driver", "C:\\WorkEnvir\\geckodriver\\geckodriver.exe");
        FirefoxDriver webDriver = new FirefoxDriver();
        webDriver.get("https://www.ly.com/flight/flight-book1.aspx?para=0*CTU*CAN*2018-02-06****all*all&key=AF9BB04B48DDA51F5F66E27FEA4B4305&TCAlianceCode=&cztype=");
        List<WebElement> flist_box = webDriver.findElementsByClassName("flist_box");
        Iterator<WebElement> it = flist_box.iterator();
        while (it.hasNext()) {
            WebElement element = it.next();
            if (!it.hasNext()) {
                element.findElement(By.className("link_orderB")).click();
                element.findElements(By.className("other_more")).forEach(WebElement::click);
            }
        }
        Document pageSource = Jsoup.parse(webDriver.getPageSource());
        List<OriginDataModel> originDataModels = new ArrayList<>();
        for (OriginDataModel model : originDataModels) {
            model.setHtml(String.valueOf(pageSource));
            AirLineModel airLineModel = new AirLineModel();
            CityModel cityModel = new CityModel();
            cityModel.setName("北京");


            airLineModel.setCityByOriginPlace(cityModel);
            airLineModel.setCityByArrivePlace(cityModel);

            model.setAirLineByAirLine(airLineModel);
        }
        System.out.println(pageSource);
    }

    private static void feizhu() {
        System.setProperty("webdriver.gecko.driver", "C:\\WorkEnvir\\geckodriver\\geckodriver.exe");
        FirefoxDriver webDriver = new FirefoxDriver();
//        webDriver.get("https://login.taobao.com/member/login.jhtml");
//
//        webDriver.findElementByCssSelector(".forget-pwd.J_Quick2Static").click();
//        webDriver.findElementByCssSelector("#TPL_username_1").sendKeys("18223205366");
//        webDriver.findElementByCssSelector("#TPL_password_1").sendKeys("zsy864939.");
//设置狐火浏览器代理：
        FirefoxProfile profile = new FirefoxProfile();
//network.proxy.type", 1   这里的1代表手工设置
        profile.setPreference("network.proxy.type", 1);
//network.proxy.http", "hzproxy.xxxx.com"  设置代理地址
        profile.setPreference("network.proxy.http", "hzproxy.xxxx.com");
//network.proxy.http", "hzproxy.xxxx.com"  设置代理端口号
        profile.setPreference("network.proxy.http_port", 8080);
        webDriver = new FirefoxDriver();

        try {
            webDriver.get("https://sjipiao.fliggy.com/flight_search_result.htm?_input_charset=utf-8&spm=181.7091613.a1z67.1001&searchBy=1280&tripType=0&depCityName=%E6%88%90%E9%83%BD&depCity=CTU&depDate=2018-02-06&arrCityName=%E5%B9%BF%E5%B7%9E&arrCity=CAN&arrDate=");

            List<WebElement> elements = webDriver.findElementsByCssSelector(".select-btn.J_SelectFlight");
            for (WebElement element : elements) {
                element.click();
                long time = (long) ((Math.random() * 1000000000) % 2000);
                Thread.sleep(time + 1500);
            }
            System.out.println(Jsoup.parse(webDriver.getPageSource()));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            webDriver.close();
        }


    }

}
